<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>c&#43;&#43; 获取时间 - Coding Night - A personal blog for Rango</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Rango" /><meta name="description" content="获取系统时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include &amp;lt;ctime&amp;gt;int main(int argc, char const *argv[]) { time_t tm = time(0); char szTime[64]; strftime(szTime, sizeof(szTime), &amp;#34;%Y/%m/%d %X %A 本年第%j天 %z&amp;#34;,localtime(&amp;amp;tm));" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://rangolee.github.io/post/cpp/c&#43;&#43;%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E6%97%B6%E9%97%B4/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.a659bb31b17a54f7792e1bc783fc75b31118e6a175b42337b03fae0b1a3ef2b5.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="c&#43;&#43; 获取时间" />
<meta property="og:description" content="获取系统时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include &lt;ctime&gt;int main(int argc, char const *argv[]) { time_t tm = time(0); char szTime[64]; strftime(szTime, sizeof(szTime), &#34;%Y/%m/%d %X %A 本年第%j天 %z&#34;,localtime(&amp;tm));" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://rangolee.github.io/post/cpp/c&#43;&#43;%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E6%97%B6%E9%97%B4/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2017-01-01T15:10:00+00:00" />
<meta property="article:modified_time" content="2017-01-01T15:10:00+00:00" />

<meta itemprop="name" content="c&#43;&#43; 获取时间">
<meta itemprop="description" content="获取系统时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include &lt;ctime&gt;int main(int argc, char const *argv[]) { time_t tm = time(0); char szTime[64]; strftime(szTime, sizeof(szTime), &#34;%Y/%m/%d %X %A 本年第%j天 %z&#34;,localtime(&amp;tm));"><meta itemprop="datePublished" content="2017-01-01T15:10:00+00:00" />
<meta itemprop="dateModified" content="2017-01-01T15:10:00+00:00" />
<meta itemprop="wordCount" content="501">
<meta itemprop="keywords" content="C&#43;&#43;," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="c&#43;&#43; 获取时间"/>
<meta name="twitter:description" content="获取系统时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include &lt;ctime&gt;int main(int argc, char const *argv[]) { time_t tm = time(0); char szTime[64]; strftime(szTime, sizeof(szTime), &#34;%Y/%m/%d %X %A 本年第%j天 %z&#34;,localtime(&amp;tm));"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Coding Night</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Coding Night</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">c&#43;&#43; 获取时间</h1>

      <div class="post-meta">
        <span class="post-time"> 2017-01-01 </span>
        <div class="post-category">
            <a href="/categories/c&#43;&#43;/"> C&#43;&#43; </a>
            </div>
          <span class="more-meta"> 约 501 字 </span>
          <span class="more-meta"> 预计阅读 1 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li>
          <ul>
            <li><a href="#获取系统时间">获取系统时间</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <!--description: MarkDown 文档的基本格式模板-->
<!--
|参数|描述|默认值|
|-----------|-----|--------------|
|layout:    |布局
|title:     |标题
|date:      |建立日期   |文件建立日期
|updated:   |更新日期   |文件更新日期
|comments:  |开启文章的评论功能    |true
|tags:      |标签（不适用于分页
|categories:|分类（不适用于分页）
|permalink: |覆盖文章网址
|description|文章描述信息
-->
<h3 id="获取系统时间">获取系统时间</h3>
<hr>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;ctime&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="k">const</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">time_t</span> <span class="n">tm</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="kt">char</span> <span class="n">szTime</span><span class="p">[</span><span class="mi">64</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="n">strftime</span><span class="p">(</span><span class="n">szTime</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">szTime</span><span class="p">),</span> <span class="s">&#34;%Y/%m/%d %X %A 本年第%j天 %z&#34;</span><span class="p">,</span><span class="n">localtime</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tm</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">    <span class="n">puts</span><span class="p">(</span><span class="n">szTime</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="cm">/**
</span></span></span><span class="line"><span class="cl"><span class="cm">     * size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
</span></span></span><span class="line"><span class="cl"><span class="cm">    根据格式字符串生成字符串。
</span></span></span><span class="line"><span class="cl"><span class="cm">    struct tm *localtime(const time_t *timer);
</span></span></span><span class="line"><span class="cl"><span class="cm">    取得当地时间，localtime获取的结果由结构tm返回
</span></span></span><span class="line"><span class="cl"><span class="cm">    返回的字符串可以依下列的格式而定：
</span></span></span><span class="line"><span class="cl"><span class="cm">    %a 星期几的缩写。Eg:Tue
</span></span></span><span class="line"><span class="cl"><span class="cm">    %A 星期几的全名。 Eg: Tuesday
</span></span></span><span class="line"><span class="cl"><span class="cm">    %b 月份名称的缩写。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %B 月份名称的全名。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %c 本地端日期时间较佳表示字符串。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %d 用数字表示本月的第几天 (范围为 00 至 31)。日期
</span></span></span><span class="line"><span class="cl"><span class="cm">    %H 用 24 小时制数字表示小时数 (范围为 00 至 23)。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %I 用 12 小时制数字表示小时数 (范围为 01 至 12)。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %j 以数字表示当年度的第几天 (范围为 001 至 366)。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %m 月份的数字 (范围由 1 至 12)。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %M 分钟。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %p 以 &#39;&#39;AM&#39;&#39; 或 &#39;&#39;PM&#39;&#39; 表示本地端时间。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %S 秒数。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %U 数字表示为本年度的第几周，第一个星期由第一个周日开始。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %W 数字表示为本年度的第几周，第一个星期由第一个周一开始。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %w 用数字表示本周的第几天 ( 0 为周日)。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %x 不含时间的日期表示法。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %X 不含日期的时间表示法。 Eg: 15:26:30
</span></span></span><span class="line"><span class="cl"><span class="cm">    %y 二位数字表示年份 (范围由 00 至 99)。
</span></span></span><span class="line"><span class="cl"><span class="cm">    %Y 完整的年份数字表示，即四位数。 Eg:2008
</span></span></span><span class="line"><span class="cl"><span class="cm">    %Z(%z) 时区或名称缩写。Eg:中国标准时间
</span></span></span><span class="line"><span class="cl"><span class="cm">    %% % 字符。
</span></span></span><span class="line"><span class="cl"><span class="cm">     */</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Rango</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2017-01-01
        
    </span>
  </p>
  
  
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/c&#43;&#43;/">C&#43;&#43;</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/cpp/xcode%E6%96%B0%E5%BB%BAcpp%E5%A4%B4%E6%96%87%E4%BB%B6%E6%97%B6%E9%BB%98%E8%AE%A4%E4%B8%BA.h%E5%90%8E%E7%BC%80/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Xcode如何创建默认后缀是 .h 的 c&#43;&#43; 头文件？</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/cshape/01_c#%E7%A8%8B%E5%BA%8F%E7%BB%93%E6%9E%84%E4%BD%93/">
            <span class="next-text nav-default">1.C#程序结构体</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:rangodev@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://rangolee.github.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2016 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span>Rango</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>








</body>
</html>
